import {
    request
} from "../../request/index.js";
import {
    login
} from "../../utils/asyncWx.js";
var redis = require("../../utils/redis.js").default;
const login_time = 7 * 24 * 60 * 60
Page({
    // 获取用户信息
    async handleGetUserInfo(e) {
        try {
            // 获取用户信息
            const {
                encryptedData,
                rawData,
                iv,
                signature,
                userInfo
            } = e.detail;
            // 获取小程序登录成功后的code
            const {
                code
            } = await login();
            const loginParams = {
                encryptedData,
                rawData,
                iv,
                signature,
                userInfo,
                code
            };
            //发送请求，获取用户的token
            const res = await request({
                url: "/users/wxLogin",
                data: loginParams,
                method: "post"
            });
            console.log(res);
            // 将token存入缓存，并跳转到上一个页面
            // wx.setStorageSync('token', res.data.data.token);
            redis.put('token', res.data.data, login_time);
            redis.put('userId', res.data.data.userId, login_time);
            redis.put('userInfo', res.data.data.userInfo, login_time);
            // wx.setStorageSync('userId', res.data.data.userId);
            // wx.setStorageSync('userInfo', res.data.data.userInfo);
            wx.navigateBack({ 
                delta: 1,
            })
        } catch (error) {
            console.log(error);
        }
    }
})